/**
 * author:袁新
 * time2018-10-19
 * vision:1.0.0
 * 用于工作流特殊组件开发，其中：
 * special_form_item：所有特殊工作流组件
 * countersignature：传阅签字组件
 */
// debugger;
var send_msg = 0;
// 控件格式
// <script src="/js/workflow/plugin/countersignature/main.js"></script>
// <textarea name="DATA_478" id="DATA_478" class="form_item special_form_item countersignature_item" data-type="textarea" title="传阅签字" value="" rich="0" orgfontsize="16" orgwidth="520" orgheight="100" style="font-size:16px;width:520px;height:100px;"></textarea>
//工作流办理页面，countersignature传阅签字组件按钮点击事件
function savefClick(type,e){
    var str = '';
    var num = e.attr('data-i');
    var val = $('.countersignature_item'+num).val();
    if(type == "4"){
        var username = workForm.tool.MacrosData.data.sYS_USERNAME;
        var timestamp = Date.parse(new Date());
        timestamp = new Date(timestamp).Format('yyyy-MM-dd');
        var chars = '<br><span style="word-break:keep-all;white-space:nowrap;color: #555555;">'+ username +'&nbsp;&nbsp;&nbsp;&nbsp;'+ timestamp +'</span>';
        e.addClass('countersignature_itemhide').after(chars);
    }else{
        if(type == "1"){
            str = '已阅';
            $('.countersignature_item'+num).val(val+str);
        }else if(type == "2"){
            str = '同意';
            $('.countersignature_item'+num).val(val+str);
        }else if(type == "3"){
            str = '不同意';
            $('.countersignature_item'+num).val(val+str);
        }else if(type == "5"){
            str = '清空';
            $('.countersignature_item'+num).val('');
        }
    }
}
function saveCSNtr_item(obj,cb){
    var str = obj.val();
    if(str){
        str = str.replace(/\n/g,'<br/>')
    }
    var feedFlag = obj.attr('name').split('DATA_')[1]||0;
    if(obj.attr('disabled')!='disabled'){
        $.ajax({
            type: "get",
            url: "workfeedback",
            dataType: 'JSON',
            data: {
                prcsId:globalData.flowRunPrcs.prcsId,
                flowPrcs:flowPrcs,
                runId:runId,
                content:str,
                file:'',
                feedFlag:feedFlag
            },
	        async:false,
            success: function (obj) {
                send_msg = 1;
                if(cb){
                    cb();
                }
            }
        });
    }else{
        if(cb){
            cb();
        }
    }

}
function workformCSingRe(runId,countersignature){
    for(var i=0;i<countersignature.length;i++){

            var thisNum = i;
            var feedFlag = countersignature.eq(i).attr('name').split('DATA_')[1]||0;
            $.ajax({
                type: "get",
                url: "/Countersignature/findFinalfeedback",
                dataType: 'JSON',
                data: {
                    runId:runId,
                    feedFlag:feedFlag
                },
                async:false,
                success: function (json) {
                    var str = '';
                    $('head').append('<style>body{    -webkit-user-select: auto!important;user-select: auto!important;}</style>')
                    for(var i=0;i<json.obj.length;i++){
                        var resdate =json.obj[i].content;
                        if(resdate){
                            resdate = resdate.replace(/\n/g,'<br/>')
                        }

                        str += '<div style="display: block;color: #555555;-webkit-touch-callout:initial;  /*系统默认菜单被禁用*/ -webkit-user-select:auto; /*webkit浏览器*/-khtml-user-select:auto; /*早期浏览器*/-moz-user-select:auto;/*火狐*/-ms-user-select:auto; /*IE10*/user-select:auto;"><span class="countersignatureContent">'+resdate +'</span>'+
                            '<span class="countersignatureUserName">('+ json.obj[i].userName +')</span>'+
                            '<span class="countersignatureEditTime">'+ json.obj[i].editTime +'</span></div>';

                    }
                    if(countersignature.eq(thisNum).attr('secret') != 1){
                        countersignature.eq(thisNum).before('<div class="countersignature_'+thisNum+'" style="width: auto;padding: 10px 6px;text-align: left;">'+ str +'</div>');
                    }
                    var tleft = countersignature.eq(thisNum).offset().left;
                    var left =  $('.countersignature_'+thisNum).offset().left;
                   // console.log(tleft);
                   // console.log(left);
                   //console.log(tleft-left);
                    $('.countersignature_'+thisNum).css("margin-left",tleft-left);

                }
            });

    }

}
var special_form_item = $('.special_form_item');
var runId = $.getQueryString("runId") || '';
$('html').append('<style>.countersignature_itemhide{display: none}</style>');
if(special_form_item.length != ''&&special_form_item.length != null&&special_form_item.length != undefined&&special_form_item.length != 0){
    var countersignature = $('.countersignature_item');
    //countersignature：传阅签字组件
    //传阅签字组件操作开始
    if($('#specialNumsElement').length == 0){
        $('body').append('<div id="specialNumsElement" style="display: none"></div>')
        if(countersignature.length != ''&&countersignature.length != null&&countersignature.length != undefined&&countersignature.length != 0){

            //工作流办理页面workform.jsp
            if(location.href.indexOf('/workflow/work/workform?') > -1||location.href.indexOf('/workflow/work/workformh5?') > -1){
                $('head').append('<style>.save_f {cursor: pointer;background: url(../../img/btn_browse_nor_03.png) no-repeat;background-size: 100% 100%;color: #768797;overflow: hidden;}.save_f{font-size: 14px;padding: 5px 8px;margin-right: 5px;    margin-top: 5px;display: inline-block;border-radius: 4px;text-decoration: none;}</style>')
                for(var i=0;i<countersignature.length;i++){
                    if(countersignature.eq(i).attr('secret') != 1) {
                        if(countersignature.eq(i).attr('disabled')!= 'disabled'){
                            var btns = '</br><a href="javascript:void(0)" id="read" class="save_f" style="text-decoration: none;" data-i="'+ i +'" onclick="savefClick(1,$(this))">已阅</a>'+
                                '<a href="javascript:void(0)" id="agree" class="save_f" style="text-decoration: none;" data-i="'+ i +'" onclick="savefClick(2,$(this))">同意</a>'+
                                '<a href="javascript:void(0)" id="disread" class="save_f" style="text-decoration: none;" data-i="'+ i +'" onclick="savefClick(3,$(this))">不同意</a>'+
                                '<a href="javascript:void(0)" id="clear" class="save_f" style="text-decoration: none;" data-i="'+ i +'" onclick="savefClick(5,$(this))">清空</a>'+
                                '<a href="javascript:void(0)" id="signature" class="save_f" style="text-decoration: none;" data-i="'+ i +'" onclick="savefClick(4,$(this))">签名</a>';
                        }else{
                            var btns = '</br><p>&nbsp;</p>';
                        }
                        countersignature.eq(i).addClass('countersignature_item'+i).after(btns)
                    }
                }
                if(countersignature.attr('secret') != 1) {
                    workformCSingRe(runId,countersignature);
                }
                //工作流查看详情、打印页面workformPreView.jsp
            }else if(location.href.indexOf('/workflow/work/workformPreView?') > -1|| location.href.indexOf('/workflow/work/workformh5PreView?') > -1){
                for(var i=0;i<countersignature.length;i++){
                    if(countersignature.eq(i).height()){
                        var style = 'min-height: '+ countersignature.eq(i).height() +'px;'
                    }else{
                        var style= '';
                    }
                    if(location.href.indexOf('/workflow/work/workformh5PreView?') > -1){
                        var width = 'width: auto;'
                    }else{
                        var width = ''
                    }
                    var thisNum = i;
                    var feedFlag = countersignature.eq(i).attr('name').split('DATA_')[1]||0;

                    $.ajax({
                        type: "get",
                        url: "/Countersignature/findFinalfeedback",
                        dataType: 'JSON',
                        data: {
                            runId:runId,
                            feedFlag:feedFlag
                        },
                        async:false,
                        success: function (json) {
                            var str = '';
                            $('head').append('<style>body{    -webkit-user-select: auto!important;user-select: auto!important;}</style>')
                            for(var i=0;i<json.obj.length;i++){
                                var resdate =json.obj[i].content;
                                if(resdate){
                                    resdate = resdate.replace(/\n/g,'<br/>')
                                }

                                str += '<div style="text-align:left;display: block;color: #555555;-webkit-touch-callout:initial;  /*系统默认菜单被禁用*/ -webkit-user-select:auto; /*webkit浏览器*/-khtml-user-select:auto; /*早期浏览器*/-moz-user-select:auto;/*火狐*/-ms-user-select:auto; /*IE10*/user-select:auto;"><span class="countersignatureContent">'+ resdate +'</span>'+
                                    '<span class="countersignatureUserName">('+ json.obj[i].userName +')</span>'+
                                    '<span class="countersignatureEditTime">'+ json.obj[i].editTime +'</span></div>';

                            }
                            if(json.obj.length == 0){
                                style += 'display: none;'
                            }else{
                                style += 'display: block;'
                            }
                            if(countersignature.eq(thisNum).attr('secret') != 1) {
                                countersignature.eq(thisNum).after('<div style="font-size:15px;' + countersignature.eq(thisNum).attr('style') + ';padding:4px 6px;' + style + width + 'height:auto;display: block;">' + str + '</div>').hide();
                            }
                        }
                    });

                }

            }else{

            }

        }
        //传阅签字组件操作结束
    }

}
